package com.zm.openapi.model;

import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/***
 * api执行返回结果
 * @param <T>
 */
//@ApiModel(value = "API统一数据包容器对象", description = "API统一数据包容器对象")
@Data
@Accessors(chain = true)
@ToString
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = -1414676375763798049L;

//    @ApiModelProperty(value = "结果状态码,0表示调用成功，非0表示调用失败", dataType = "String")
    private String code = "0";

//    @ApiModelProperty(value = "返回的消息", dataType = "String")
    private String msg = "成功";
//    @ApiModelProperty(value = "总数量, 分页请求时有意义", dataType = "int")
    // 总数量,分页请求时有意义
    private int total;

//    @ApiModelProperty(value = "总页数, 分页请求时有意义", dataType = "int")
    // 总页数,分页请求时有意义
    private int totalPage;

//    @ApiModelProperty(value = "当前页码, 分页请求时有意义", dataType = "int")
    // 当前页码,分页请求时有意义
    private int pageNo;

//    @ApiModelProperty(value = "每页条数, 分页请求时有意义", dataType = "int")
    // 每页条数,分页请求时有意义
    private int pageSize;

//    @ApiModelProperty(value = "返回的具体数据对象", dataType = "object")
    //数据
    private T data;

//    @ApiModelProperty(value = "是否是集合,包括数组，集合, Map", dataType = "boolean")
    //是否是集合,包括数组，集合, Map
    private boolean collectionFlag = false;

    /**
     * 隐藏消息，便于调试
     */
//    @ApiModelProperty(value = "隐藏消息，便于调试", dataType = "String")
    private String hiddenMsg;

    /**
     * 业务验证失败的字段名
     */
//    @ApiModelProperty(value = "业务验证失败的字段名", dataType = "String")
    private String invalidField;

    /**
     * 服务器时间戳，单位：毫秒
     */
//    @ApiModelProperty(value = "服务器时间戳，单位：毫秒", dataType = "Long")
    private Long serviceTime = System.currentTimeMillis();

    public ApiResult() {
    }

    public ApiResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResult(IResultEnum myEnum, String extraErrorMsg, String hiddenMsg) {
        this(myEnum, extraErrorMsg, hiddenMsg, null);
    }

    public ApiResult(IResultEnum myEnum, String extraErrorMsg, String hiddenMsg, String invalidField) {
        if (myEnum != null) {
            this.code = myEnum.getCode();
            this.msg = myEnum.getMsg();
        }
        if (StringUtils.isNotBlank(extraErrorMsg)) {
            this.msg = this.msg + ", " + extraErrorMsg;
        }
        this.hiddenMsg = hiddenMsg;
        this.invalidField = invalidField;
    }

    public boolean isSuccess() {
        return "0".equals(code);
    }
}
